package com.test.burnincash.view_burnin.module_burnin;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.test.burnincash.R;
import com.test.burnincash.bean_burnin.process_burnin.ProcessInfo_burnin;
import com.test.burnincash.constant_burnin.Constant_burnin;
import com.test.burnincash.util_burnin.StringUtil_burnin;

import java.util.ArrayList;

public class ProcessView_burnin extends RelativeLayout {

    public ProcessView_burnin(Context context) {
        super(context);
        initView(context);
    }

    public ProcessView_burnin(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ProcessView_burnin(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context){

        View view = View.inflate(context, R.layout.view_process_burnin,this);

        LinearLayout pointGroupLayout = view.findViewById(R.id.view_process_layout_point_group);
        LinearLayout textGroupLayout = view.findViewById(R.id.view_process_layout_text_group);

        ArrayList<String> allSteps = ProcessInfo_burnin.getInstance().getAllSteps();
        ArrayList<String>  allStepsTitle = ProcessInfo_burnin.getInstance().getAllStepTitles();
        ArrayList<String> steps = ProcessInfo_burnin.getInstance().getSteps();

        if (steps==null||steps.isEmpty()) return;

        boolean nowProcessFlag = true;
        for (int i=0;i<allSteps.size();i++){

            View textView = View.inflate(context,R.layout.view_process_text_burnin,null);
            TextView textTv = textView.findViewById(R.id.view_process_text_tv_text);
            textView.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT,1));
            textGroupLayout.addView(textView);

            textTv.setText(StringUtil_burnin.processTitleFormat(allSteps.get(i)));

            View pointParetnView = View.inflate(context,R.layout.view_process_point_burnin,null);
            View pointIv = pointParetnView.findViewById(R.id.view_process_point_view_point);

            View lineParentView = View.inflate(context,R.layout.view_process_line_burnin,null);
            View lineView = lineParentView.findViewById(R.id.view_process_line_view_line);
            lineParentView.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT,1));

            //已完成
            if (!steps.contains(allSteps.get(i))){
                pointIv.setBackgroundResource(R.drawable.ic_bg_process_point_finish);
                lineView.setBackgroundResource(R.drawable.ic_bg_process_line_finish);
                textTv.setTextColor(ContextCompat.getColor(context,R.color.green_burnin));
            }else
                //进行中
                if (steps.contains(allSteps.get(i))&&nowProcessFlag){
                    pointIv.setBackgroundResource(R.drawable.ic_bg_process_point_now);
                    textTv.setTextColor(ContextCompat.getColor(context,R.color.black_burnin));
                    nowProcessFlag = false;
                }

            pointGroupLayout.addView(pointParetnView);

            if (i<allSteps.size()-1){

                pointGroupLayout.addView(lineParentView);

            }else if ((i==allSteps.size()-1)){

                pointParetnView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {

                        pointParetnView.getViewTreeObserver().removeOnPreDrawListener(this);

                        int groupWidth = pointGroupLayout.getWidth();
                        int pointWidth = pointParetnView.getWidth();
                        if (pointWidth<(groupWidth/allSteps.size())){
                            int padding = ((groupWidth/allSteps.size())-pointWidth)/2;
                            pointGroupLayout.setPadding(padding,0,padding,0);
                        }

                        return false;
                    }
                });

            }
        }

    }

}
